package com.lv.common.utils.picengine;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;
import com.luck.picture.lib.engine.CropFileEngine;
import com.yalantis.ucrop.UCrop;
import com.yalantis.ucrop.UCropImageEngine;
import com.yalantis.ucrop.model.AspectRatio;

import java.util.ArrayList;

/**
 * 作者： ygx
 * 创建日期：2022/9/13 10:11 AM
 * 签名： 天行健，君子以自强不息；地势坤，君子以厚德载物。
 * -    _              _           _     _   ____  _             _ _
 * -   / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 * -  / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 * - / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 * -/_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 * <p>
 * You never know what you can do until you try !
 * ----------------------------------------------------------------
 */
public class CropWHEngine implements CropFileEngine {

    private float ratioX = 16;
    private float ratioy = 9;
    private String mTitle = "16:9";

    private CropWHEngine() {
    }

    public void setRatio(String title, float x, float y) {
        mTitle = title;
        ratioX = x;
        ratioy = y;
    }

    @Override
    public void onStartCrop(Fragment fragment, Uri srcUri, Uri destinationUri, ArrayList<String> dataSource, int requestCode) {
//        UCrop uCrop = UCrop.of(srcUri, destinationUri);

//        UCrop.Options options = buildOptions();
        UCrop uCrop = UCrop.of(srcUri, destinationUri, dataSource);
        UCrop.Options options = new UCrop.Options();
        uCrop.setImageEngine(new UCropImageEngine() {
            @Override
            public void loadImage(Context context, String url, ImageView imageView) {
//                if (!ImageLoaderUtils.assertValidRequest(context)) {
//                    return;
//                }
                Glide.with(context).load(url).override(180, 180).into(imageView);
            }

            @Override
            public void loadImage(Context context, Uri url, int maxWidth, int maxHeight, OnCallbackListener<Bitmap> call) {
                Glide.with(context).asBitmap().load(url).override(maxWidth, maxHeight).into(new CustomTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                        if (call != null) {
                            call.onCall(resource);
                        }
                    }

                    @Override
                    public void onLoadCleared(@Nullable Drawable placeholder) {
                        if (call != null) {
                            call.onCall(null);
                        }
                    }
                });
            }
        });
//        options.setCircleDimmedLayer(true);//圆形裁剪
        options.setStatusBarColor(Color.WHITE);//状态栏背景色
        options.isDarkStatusBarBlack(true);//true 状态栏文字黑
        options.setMultipleCropAspectRatio(buildAspectRatios(dataSource.size()));
//        options.setHideBottomControls(false);//底部控制器
        options.setFreeStyleCropEnabled(false);//设置裁剪框是否可修改
        options.setShowCropFrame(true);//是否显示裁剪边框
        options.setShowCropGrid(true);//显示网格
        options.withAspectRatio(ratioX, ratioy);//设置裁剪长宽比
//        options.setCropOutputPathDir(getSandboxPath());
        options.isCropDragSmoothToCenter(false);
//        options.setSkipCropMimeType(getNotSupportCrop());
        options.isForbidCropGifWebp(true);//跳过Gif
        options.isForbidSkipMultipleCrop(false);//是否可跳过裁剪
        options.setMaxScaleMultiplier(100);
        uCrop.withOptions(options);
        uCrop.start(fragment.requireActivity(), fragment, requestCode);
    }

    /**
     * 多图裁剪时每张对应的裁剪比例
     *
     * @param dataSourceCount
     * @return
     */
    private AspectRatio[] buildAspectRatios(int dataSourceCount) {
        AspectRatio[] aspectRatios = new AspectRatio[dataSourceCount];
        for (int i = 0; i < dataSourceCount; i++) {
            aspectRatios[i] = new AspectRatio(mTitle, ratioX, ratioy);
//            if (i == 0) {
//                aspectRatios[i] = new AspectRatio("16:9", 16, 9);
//            } else if (i == 1) {
//                aspectRatios[i] = new AspectRatio("3:2", 3, 2);
//            } else {
//                aspectRatios[i] = new AspectRatio("原始比例", 0, 0);
//            }
        }
        return aspectRatios;
    }


    private static final class InstanceHolder {
        static final CropWHEngine instance = new CropWHEngine();
    }

    public static CropWHEngine createCropEngine() {
        return InstanceHolder.instance;
    }

}